/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.wizards.jcproject.pages;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage;

/**
 * @author Amgad S Youssef
 *
 */
public class NewJCProjectJavaCapabilityConfigurationPage extends JavaCapabilityConfigurationPage {
	private NewJCProjectNameWizardPage newJCProjectNameWizardPage;

	public NewJCProjectJavaCapabilityConfigurationPage(NewJCProjectNameWizardPage newJCProjectNameWizardPage) {
		super();
		this.newJCProjectNameWizardPage = newJCProjectNameWizardPage;
	}

	@Override
	public void setVisible(boolean visible) {
		try {
			IJavaProject jcProject = newJCProjectNameWizardPage.getCreatedJCProject();
			if (visible) {
				if (jcProject == null || !jcProject.exists()) {
					newJCProjectNameWizardPage.createJavacardProject(null);
					jcProject = newJCProjectNameWizardPage.getCreatedJCProject();
					init(jcProject, jcProject.getOutputLocation(), jcProject.getRawClasspath(), true);
					configureJavaProject(null);
				}
			} else {
				if (jcProject != null && jcProject.exists()) {
					jcProject.getProject().delete(true, true, null);
				}
			}
		} catch (CoreException e) {
			setErrorMessage(e.getMessage());
		} catch (InterruptedException e) {
			setErrorMessage(e.getMessage());
		}
		super.setVisible(visible);
	}

}
